<?php require_once('../Connections/CrossFit.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$colname_CheckUserName = "-1";
if (isset($_POST['Email'])) {
  $colname_CheckUserName = $_POST['Email'];
}
mysql_select_db($database_CrossFit, $CrossFit);
$query_CheckUserName = sprintf("SELECT Username FROM Login WHERE Username = %s", GetSQLValueString($colname_CheckUserName, "text"));
$CheckUserName = mysql_query($query_CheckUserName, $CrossFit) or die(mysql_error());
$row_CheckUserName = mysql_fetch_assoc($CheckUserName);
$totalRows_CheckUserName = mysql_num_rows($CheckUserName);

$colname_MemberCheck = "-1";
if (isset($_POST['Email'])) {
  $colname_MemberCheck = $_POST['Email'];
}
mysql_select_db($database_CrossFit, $CrossFit);
$query_MemberCheck = sprintf("SELECT Email FROM Members WHERE Email = %s", GetSQLValueString($colname_MemberCheck, "text"));
$MemberCheck = mysql_query($query_MemberCheck, $CrossFit) or die(mysql_error());
$row_MemberCheck = mysql_fetch_assoc($MemberCheck);
$totalRows_MemberCheck = mysql_num_rows($MemberCheck);




if($row_CheckUserName['Username'] == $_POST['Email']){ //check if there is already an entry for that username
mysql_free_result($CheckUserName);
mysql_free_result($MemberCheck);

 header('Location: http://development.crossfit26.com/app/registration-issue/');
} else {

 	if ($row_MemberCheck['Email'] <> $_POST['Email'] ){ 
	mysql_free_result($CheckUserName);
    mysql_free_result($MemberCheck);
	
	header('Location: http://development.crossfit26.com/app/registration-issue/');
	} else {

		$editFormAction = $_SERVER['PHP_SELF'];
		if (isset($_SERVER['QUERY_STRING'])) {
 			 $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
			}

			$activationKey =  mt_rand();

			if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form")) {
 			 $insertSQL = sprintf("INSERT INTO Login (Username, Password, Active) VALUES (%s, SHA1(%s), '$activationKey')",
                       GetSQLValueString($_POST['Email'], "text"),
                       GetSQLValueString($_POST['Password'], "text"),
                       GetSQLValueString($_POST['Active'], "int"));

  



 			 $updateSQL = sprintf("UPDATE Members SET Phone=%s, Address=%s, City=%s, `State`=%s, Zip=%s, Emergency_Contact=%s, 			Emergency_Contact_Phone=%s WHERE Email=%s",
                       GetSQLValueString($_POST['PhoneNumber'], "text"),
                       GetSQLValueString($_POST['Address'], "text"),
                       GetSQLValueString($_POST['City'], "text"),
                       GetSQLValueString($_POST['State'], "text"),
                       GetSQLValueString($_POST['ZipCode'], "text"),
                       GetSQLValueString($_POST['EmergencyContactName'], "text"),
                       GetSQLValueString($_POST['EmergencyContactPhoneNumber'], "text"),
                       GetSQLValueString($_POST['Email'], "text"));

  mysql_select_db($database_CrossFit, $CrossFit);
  $Result1 = mysql_query($insertSQL, $CrossFit) or die(mysql_error());
  $Result2 = mysql_query($updateSQL, $CrossFit) or die(mysql_error());

##Send activation Email to Owner

$to      = "ryan@crossfit26.com";

$subject = "CrossFit 26 Member Registration";

$message = $_POST['Email'] . " has registered to access their member profile at http://members.crossfit26.com.";

$headers = 'From: ryan@crossfit26.com' . "\r\n";

    

mail($to, $subject, $message, $headers);

##Send activation Email to User

$to      = $_POST['Email'];

$subject = "CrossFit 26 Member Registration";

$message = "Welcome to the CrossFit 26 Member site, Let's get it!\r\rYou need to complete registration to access your profile at 26. You can complete registration by clicking the following link:\rhttp://development.crossfit26.com/process/confirmregistration.php?code=$activationKey\r\rIf you have recieved this message erroneously, please contact ryan@crossfit26.com.\r\rRegards, the CrossFit 26 Team";

$headers = 'From: ryan@crossfit26.com' . "\r\n";

    

mail($to, $subject, $message, $headers);
 
}
	}
}
 


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Process Registration</title>
</head>

<body>
<center><img src="ajax-loader.gif" width="32" height="32" style="margin-top:150px;"><p>Processing...</p>

<p>
 
</p>
</center>
<?php
mysql_free_result($CheckUserName);

mysql_free_result($MemberCheck);
?>
<script type="text/javascript">
<!--
window.location = "http://development.crossfit26.com/app/registration-pending/"
//-->
</script>


</body>
</html>
